﻿
Imports std

Public Class ping
    Public Const functionName As String = "ping"
    Private Shared replacer As vector(Of pair(Of String, String)) = Nothing

    Public Class query
        Public Const key As String = "k"
        Public Const operation As String = "o"
    End Class

    Public Class operations
        Public Const increase As String = "i"
        Public Const decrease As String = "d"
    End Class

    Private Shared Function generateUrl(ByVal perceptron As String _
                                       , ByVal key As String, ByVal operation As String) As String
        Return "'" + urlUtils.appendQuery(perceptron _
                                          , query.key + network.argumentNameValueSeparator + "'+" + key + "+'" _
                                          + network.argumentSeperator _
                                          + query.operation + network.argumentNameValueSeparator + "'+" + operation)
    End Function

    Public Shared Sub invoke(ByVal inlineScript As inlineScript, ByVal perceptron As String _
                             , ByVal key As String, ByVal increase As Boolean, ByRef actionTarget As String _
                             , Optional ByVal perceptronOverride As String = Nothing)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        assert(Not isemptystring(perceptron), "perceptron is an empty string.")
        assert(Not isemptystring(key), "key is an empty string.")

        If isemptystring(perceptronOverride) Then
            copy(perceptronOverride, perceptron)
            strrplc2(perceptronOverride, replacer, False)
        End If

        inlineScript.addscript("ping_" + perceptron _
                               , "function " + functionName + "_" + perceptronOverride + "(k,o)" _
                               + "{(new image).src=" + generateUrl(perceptron, "k", "o") + ";}")

        Dim operation As String = Nothing
        If increase Then
            operation = operations.increase
        Else
            operation = operations.decrease
        End If
        actionTarget += functionName + "_" + perceptronOverride + "(" + key + ",'" + operation + "')"
    End Sub

    Shared Sub New()
        replacer = New vector(Of pair(Of String, String))()
        replacer.push_back(make_pair(Of String, String)(character.colon, character.underline))
        replacer.push_back(make_pair(Of String, String)(character.leftSlash, character.underline))
        replacer.push_back(make_pair(Of String, String)(character.dot, character.underline))
        replacer.push_back(make_pair(Of String, String)(character.percentMark, character.underline))
    End Sub
End Class
